package com.gk.javaservice.util.serialTool;

import gnu.io.SerialPort;

public class Write {

    public static void main(String[] args) {

        SerialPort serialPort = null;                  //打开的端
        try {
            //打开串口
            serialPort = SerialTool.openPort("COM3", 115200);
            //数组“Hello World！”
            byte[] bs = new byte[]{'H','e','l','l','o',' ','W','o','r','l','d','!'};
            //写入数组
            SerialTool.sendToPort(serialPort, bs);
            // System.out.println(bs);

            byte[] bs2= SerialTool.readFromPort(serialPort);
            // System.out.println(bs2);
        } catch (SerialPortParameterFailure | NotASerialPort | NoSuchPort | PortInUse | SendDataToSerialPortFailure | SerialPortOutputStreamCloseFailure e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ReadDataFromSerialPortFailure readDataFromSerialPortFailure) {
            readDataFromSerialPortFailure.printStackTrace();
        } catch (SerialPortInputStreamCloseFailure serialPortInputStreamCloseFailure) {
            serialPortInputStreamCloseFailure.printStackTrace();
        } finally {
            //关闭串口
            SerialTool.closePort(serialPort);
        }
    }

}
